function OS_Stage(game)
{
	var canvas=null;
	var container=null;
	var context=null;
	var viewCenter={};
	
	var initCanvas=function()
	{
		container=document.getElementById(game.config.gameContainer);
		container.innerHtml="";
		canvas=document.createElement("canvas");
		canvas.width=game.config.width;
		canvas.height=game.config.height;
		container.appendChild(canvas);
		context=canvas.getContext("2d");
		game.ready("canvas");
	}
	
	window.addEventListener("load",initCanvas,false);
	
	this.setViewCenter=function(x,y)
	{
		context.save();
		//对于宽度边界小于屏幕宽度的情况，将中心视点置为地图X轴中心
		if (game.status.activeInterface.world.colCount*game.config.squarewidth < game.config.width)
		{
			x=game.status.activeInterface.world.colCount*game.config.squarewidth/2;
		}
		else if (x<game.config.width/2)
		{
			x=game.config.width/2;
		}
		else if (x>game.status.activeInterface.world.colCount*game.config.squarewidth-game.config.width/2)
		{
			x=game.status.activeInterface.world.colCount*game.config.squarewidth-game.config.width/2;
		}
		
		//同理 将中心视点置于Y轴中心
		if (game.status.activeInterface.world.rowCount*game.config.squareheight < game.config.height)
		{
			y=game.status.activeInterface.world.rowCount*game.config.squareheight/2;
		}
		else if (y<game.config.height/2)
		{
			y=game.config.height/2;
		}
		else if (y>game.status.activeInterface.world.rowCount*game.config.squareheight-game.config.height/2)
		{
			y=game.status.activeInterface.world.rowCount*game.config.squareheight-game.config.height/2;
		}
		viewCenter.x=x;
		viewCenter.y=y;
		context.translate(
			game.config.width/2-x,
			game.config.height/2-y
		);
	}
	
	this.resetViewCenter=function()
	{
		context.restore();
	}
	
	this.drawSquare=function(color, left, top)
	{
		context.fillStyle=color;
		context.fillRect(left,top,game.config.squarewidth,game.config.squareheight);
	}

	this.draw=function(bitmap, left, top, c)
	{
		var contextTemp=context;
		if (!!c) contextTemp=c;
		contextTemp.drawImage(
			bitmap.source.getImage(),
			bitmap.offsetx,
			bitmap.offsety,
			bitmap.width,
			bitmap.height,
			left,
			top,
			bitmap.width+0.5,
			bitmap.height+0.5);
	}
	
	var box4=[[{},{}],[{},{}]];
	box4[0][0].checks=[[-1,0],[0,-1],[-1,-1]];//4 2 1
	box4[0][0].offsets=[{row:0,col:0},{row:0,col:0},{row:1,col:1},{row:1,col:1},{row:2,col:0},{row:2,col:0},{row:0,col:1},{row:2,col:1}];		
	box4[0][1].checks=[[-1,0],[0,1],[-1,1]];
	box4[0][1].offsets=[{row:0,col:0},{row:0,col:0},{row:1,col:0},{row:1,col:0},{row:2,col:1},{row:2,col:1},{row:0,col:1},{row:2,col:0}];		
	box4[1][0].checks=[[1,0],[0,-1],[1,-1]];
	box4[1][0].offsets=[{row:0,col:0},{row:0,col:0},{row:2,col:1},{row:2,col:1},{row:1,col:0},{row:1,col:0},{row:0,col:1},{row:1,col:1}];		
	box4[1][1].checks=[[1,0],[0,1],[1,1]];
	box4[1][1].offsets=[{row:0,col:0},{row:0,col:0},{row:2,col:0},{row:2,col:0},{row:1,col:1},{row:1,col:1},{row:0,col:1},{row:1,col:0}];	
	
	this.fix=function(map,layer,row,col,c)
	{
		var contextTemp=context;
		if (!!c) contextTemp=c;
		var bitmap=map.mapdata[row][col][layer];
		var left=game.config.squarewidth*col;
		var top=game.config.squareheight*row;
		var exlen=0.5;
		var halfsw=game.config.squarewidth>>1;
		var halfsh=game.config.squareheight>>1;
		
		for (var ii=0;ii<2;ii++)
		{
			for (var jj=0;jj<2;jj++)
			{
				var res=0;
				for (var i=0;i<3;i++)
				{
					var condition_row=row+box4[ii][jj].checks[i][0];
					var condition_col=col+box4[ii][jj].checks[i][1];
					if (condition_row<0 || condition_col<0 ||
						condition_row>=map.rowCount || condition_col>=map.colCount ||
						map.mapdata[condition_row][condition_col][layer]===bitmap)
					{
						var temp=4;
						for (var j=0;j<i;j++) temp=temp>>1;
						res+=temp;
					}
				}
				var offset=box4[ii][jj].offsets[res];
				contextTemp.drawImage(
						bitmap.source.getImage(),
						bitmap.offsetx+game.config.squarewidth*offset.col+jj*halfsw,
						bitmap.offsety+game.config.squareheight*offset.row+ii*halfsh,
						halfsw,
						halfsh,
						left+jj*halfsw,
						top+ii*halfsh,
						halfsw+exlen,
						halfsh+exlen
					);
			}//for
		}
	}
	
	this.drawWindow=function(win,left,top,width,height,flag)
	{
		win.drawSelf(context,left,top,width,height,flag);
	}
	
	this.getContext=function()
	{
		return context;
	}
	
	this.write=function(text, left, top)
	{
		if (!!text.background)
		{
			var paddingL=10;
			var backH=22;
			if (!!text.size) backH=Number(text.size)+10;
			var backW=this.measureText(text).width;
			context.fillStyle=text.background;
			var backL=left-paddingL;
			if (text.align=="right") backL=backL-backW;
			if (text.align=="center") backL=backL-backW*0.5;
			context.fillRect(backL,top,backW+paddingL*2,backH);
		}
		if (game.userAgent=="Firefox") top+=5;//浏览器兼容
		context.fillStyle="#000";
		if (!!text.color) context.fillStyle=text.color;
		var perfix="";
		if (text.bold===true) perfix="bold ";
		context.shadowColor="transparent";
		if (!!text.shadow)
		{
			context.shadowOffsetX=text.shadow.offsetX;
			context.shadowOffsetY=text.shadow.offsetY;
			context.shadowBlur=text.shadow.blur;
			context.shadowColor=text.shadow.color;
		}
		var fontSize=12;
		if (!!text.size) fontSize=text.size;
		var fontFamily="Microsoft YaHei,sans-serif";
		if (!!text.family) fontFamily=text.family;
		context.font=perfix+fontSize+"px "+fontFamily;
		context.textAlign="left";
		if (!!text.align){context.textAlign=text.align;}
		context.textBaseline="top";
		if (!!text.baseLine){context.textBaseline=text.baseLine;}
		if (!!text.maxWidth)
		{
			context.fillText(text.content, left, top, text.maxWidth);
		}
		else
		{
			context.fillText(text.content, left, top);
		}
		context.shadowColor="transparent";
	}
	
	this.measureText=function(text)
	{
		var perfix="";
		if (text.bold===true) perfix="bold ";
		var fontSize=12;
		if (!!text.size) fontSize=text.size;
		var fontFamily="Microsoft YaHei,sans-serif";
		if (!!text.family) fontFamily=text.family;
		context.font=perfix+fontSize+"px "+fontFamily;
		return context.measureText(text.content);
	}
	
	this.fill=function(param)
	{
		if (param!=undefined)
		{
			//说明这是一个image资源
			if (!!param.getImage)
			{
				var image=param.getImage();
				var width=image.width;
				var height=image.height;
				height=height*game.config.width/width;
				width=game.config.width;
				if (height<game.config.height)
				{
					width=width*game.config.height/height;
					height=game.config.height;
				}
				var left=game.config.width*0.5-width*0.5;
				var top=game.config.height*0.5-height*0.5;
				context.drawImage(image,0,0,image.width,image.height,left,top,width,height);
			}
			//这是一个字符串
			else if (typeof param=="string")
			{
				context.fillStyle=param;
				context.fillRect(0,0,game.config.width,game.config.height);
			}
		}
	}
	
	this.clear=function(param)
	{
		canvas.width=canvas.width;
		this.fill(param);
	}
}
